import router from './index';
import store from '@/store/index';
router.beforeEach(async (to,from,next)=>{
    //用户没有登录
    if(!store.getters.token) {
        if(to.name === 'login') next()
        else next({ name: 'login'})
    //用户已经登录
    } else {
        /*判断token失效没有*/
        if(store.getters.toKenExpire < new Date().getTime()) {
            store.dispatch('user/loginOut');
            return;
        }
        /*从getter获取用户头像信息*/
        const avatar = store.getters.avatar;
        /*有没有刷新*/
        if(avatar) return next();
        const rote = await store.dispatch('user/getUserAvatar');
        const accessRoutes = await store.dispatch('permission/getAuthRouter',rote,{ root: true });
        accessRoutes.forEach((item:any,index:any)=> {
            router.addRoute(item);
        })
        /*进行这个操作replace标识不能通过浏览器返回*/
        next({...to,replace:true})
    }
})
